package com.demo.web;

import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.Com;
import com.demo.service.IComService;
import com.demo.service.IDepService;
import com.demo.service.IUserService;
import com.demo.vo.ComVo;
import com.demo.vo.DepVo;
import com.demo.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/com")
public class ComController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IComService comService;
    @Autowired
    private IDepService depService;
    @Autowired
    private IUserService userService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(Com arg) {
        Com com = comService.selectByComName(arg.getName().trim());
        if (com != null) {
            return new BaseResult(false, "单位名称已存在!", 403);
        }
        int service_res = comService.insertSelective(arg);
        BaseResult res = new BaseResult(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Com arg) {

        DepVo dv = new DepVo();
        UserVo uv = new UserVo();
        BaseResult<Object> res;
        dv.setComid(String.valueOf(arg.getId()));
        uv.setComid(arg.getId());
        if (depService.queryCountByComId(dv) > 0) {
            res = new BaseResult<Object>(false, "该公司关联部门，不能删除!", 403);
        } else if (userService.queryCountByComId(uv) > 0) {
            res = new BaseResult<Object>(false, "该公司关联用户，不能删除!", 403);
        } else {
            int service_res = comService.deleteByPrimaryKey(arg.getId());
            res = new BaseResult<Object>(true, service_res);
        }
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Com arg) {
        int service_res = comService.updateByPrimaryKeySelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(ComVo arg) {
        List<Com> list = comService.queryByPage(arg);
        int total = comService.queryCount(arg);
        arg.getPage();
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        System.out.println("Size:" + arg.getSize());
        System.out.println("getSortField:" + arg.getSortField());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
